// pages/route/route.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:'driving',
    typeArr:[
      {
        title:'驾车',
        id:'driving'
      },
      {
        title:'步行',
        id:'walking'
      },
      {
        title:'骑行',
        id:'bicycling'
      },
      {
        title:'电动车',
        id:'ebicycling'
      },{
        title:'公交',
        id:"transit"
      }
    ],
    circuit:0,
    polyline:[],
    dataArr:[],
    transitData:[],
    lat:'',
    lng:'',
    toLat:'',
    toLng:'',
  },
  getNowSeat(){
      wx.request({
     url: 'https://apis.map.qq.com/ws/location/v1/ip?key=HZNBZ-XPNRV-TP3PC-5KHSW-LGBQZ-YCF7Q',
     success:({data})=>{
       let {result:{location:{lat,lng}}} = data
       this.setData({lat,lng})
     },
   })
  },
  getRouter(){
    let type = this.data.type
    let circuit = this.data.circuit
    return wx.request({
      url: `https://apis.map.qq.com/ws/direction/v1/${type}/?from=${this.data.lat},${this.data.lng}&to=${this.data.toLat},${this.data.toLng}&key=HZNBZ-XPNRV-TP3PC-5KHSW-LGBQZ-YCF7Q&get_mp=1`,
      success:({data})=>{
        if(this.data.type=='transit'){
          console.log(data);
          if(data.status == 326){
            return
          }
          let dataArr = data.result.routes
          console.log(dataArr);
          let allDataArr = []
          dataArr.forEach(item => {
            let dataObj = {}
            dataObj.distance = item.distance>=1000? (item.distance/1000).toFixed(2) + '公里': item.distance+'米';
            dataObj.duration =  item.duration>60?parseInt(item.duration/60)+'小时'+item.duration%60 + '分钟':item.duration+'分钟'
            dataObj.arr = []
            item.steps.forEach(i => {
              let obj = {}
              obj.show = false
              if(i.mode == 'WALKING'){
                if(i.steps){
                  obj.isSteps = true
                  obj.steps = i.steps
                }
                obj.title  = '步行'
                obj.distance = i.distance>=1000? (i.distance/1000).toFixed(2) + '公里': i.distance+'米';
                obj.duration =  i.duration>60?parseInt(i.duration/60)+'小时'+i.duration%60 + '分钟':i.duration+'分钟'
              } else if(i.mode == 'TRANSIT'){
                obj.lines = i.lines
                if(i.lines[0].vehicle == 'BUS'){
                  obj.title = '公交'
                }else if(i.lines[0].vehicle == 'SUBWAY'){
                  obj.title = '地铁'
                }else if(i.lines[0].vehicle == 'RAIL'){
                  obj.title = '铁路'
                }
              }
              dataObj.arr.push(obj)
            })
            allDataArr.push(dataObj)
          })
            this.setData({transitData:allDataArr})
            console.log(this.data.transitData);
        }else{
          data.result.routes.forEach(item => {
            item.distance = item.distance>=1000? (item.distance/1000).toFixed(2) + '公里': item.distance+'米';
            item.duration = item.duration>60?parseInt(item.duration/60)+'小时'+item.duration%60 + '分钟':item.duration+'分钟'
          })
          let coors = data.result.routes[circuit].polyline
          let pl = []
          var kr = 1000000;
          // 解压
          for (var i = 2; i < coors.length; i++) {
            coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
          }
          // 将解压后的坐标放入点串数组pl中
          for (var i = 0; i < coors.length; i += 2) {
            pl.push({ latitude: coors[i], longitude: coors[i + 1] })
          }
          this.setData({ 
            dataArr:data.result.routes,
            // 绘制路线
            polyline: [{
              points: pl,
              color: '#58c16c',
              width: 6,
              borderColor: '#2f693c',
              borderWidth: 1
            }]})
        }
      }
    })
  },
  chgScheme(e){
    if(e.currentTarget.dataset.num == '1'){
      let circuit = e.currentTarget.dataset.scheme
      this.setData({circuit})
    }else{
      let type = e.currentTarget.dataset.type
      this.setData({type,circuit:0})
    }
    this.getRouter()
  },
  /**
   * 生命周期函数--监听页面加载
   */
 onLoad  (options) {
  this.setData({toLat:options.lat,toLng:options.lng,})
     this.getNowSeat()
     setTimeout(()=>{
       this.getRouter()
     },500)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:async ()=> {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})